//
//  StudyPoseTipView.swift
//  SitPosition
//
//  Created by apple on 2019/2/8.
//  Copyright © 2019 apple. All rights reserved.
//

import Foundation
import UIKit


/**
 * 坐姿状态提示视图
 */
class StudyPoseTipView : UIView {
    
    // 点击事件
    public var onTap: () -> Void = { }
    
    // 当前是否检测到人？
    public var hasHuman: Bool = false {
        didSet {
            _updatePoseTip()
        }
    }
    // 暂无数据
    public var noData: Bool = true {
        didSet { _updatePoseTip() }
    }
    // 当前的坐姿类型
    public var type: PoseType = PoseType.ok {
        didSet { _updatePoseTip() }
    }
    
    // 正确坐姿持续时间
    public var correctPoseDuration: TimeInterval = 0 {
        didSet {
            _updatePoseCorrectTip()
        }
    }
    
    private lazy var button: UIButton = {
        let btn = UIButton(type: UIButton.ButtonType.custom)
        btn.titleLabel?.font = Theme.Font.regularFont(ofSize: 14)
        btn.setTitleColor(UIColor.white, for: UIControl.State.normal)
        btn.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 4)
        btn.addTarget(self, action: #selector(_handleTap), for: UIControl.Event.touchUpInside)
        return btn
    }()
    
    override func willMove(toSuperview newSuperview: UIView?) {
        super.willMove(toSuperview: newSuperview)
        if button.superview == nil {
            addSubview(button)
            _updatePoseTip()
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        button.frame = bounds
    }
    
}


// MARK: - Private Methods

extension StudyPoseTipView {
    
    // 将秒转换为可阅读的字符串
    private static func _readableTimeString(_ ti: TimeInterval) -> String {
        var string: String = ""
        let h: Int = Int(ti)/3600
        let m: Int = (Int(ti) % 3600)/60
        if h > 0 {
            string.append("\(h)\(Strings.unitHours)")
        }
        if m > 0 {
            string.append("\(m)\(Strings.unitMinutes)")
        }
        return string.isEmpty ? "0\(Strings.unitMinutes)":string
    }
    
    // 更新姿态正确情况的提示语
    private func _updatePoseCorrectTip() {
        let placeholder: String
        if correctPoseDuration > 0 {
            let durationStr: String = StudyPoseTipView._readableTimeString(correctPoseDuration)
            placeholder = "（已学习\(durationStr)）"
        } else {
            placeholder = ""
        }
        let s: String = Strings.studyPoseCorrectTip.replacingOccurrences(of: "$DURATION", with: placeholder)
        button.setTitle(s, for: UIControl.State.normal)
    }
    
    // 更新姿态错误情况的提示语
    private func _updatePoseWrongTip() {
        let placeholder: String = type.description
        let s: String = Strings.studyPoseWrongTip.replacingOccurrences(of: "$SLANT", with: placeholder)
        button.setTitle(s, for: UIControl.State.normal)
    }
    
    // 更新姿态提示语
    private func _updatePoseTip() {
        guard hasHuman && !noData else {
            button.backgroundColor = UIColor(hexString: "#FF7A46", alpha: 1.0)
            button.setImage(UIImage(named: "study_error"), for: UIControl.State.normal)
            if noData {
                button.setTitle(Strings.studyNoDataTip, for: UIControl.State.normal)
            } else {
                button.setTitle(Strings.studyNoHumanTip, for: UIControl.State.normal)
            }
            return
        }
        switch type {
        case .ok:
            button.backgroundColor = UIColor(hexString: "#67DC54", alpha: 1.0)
            button.setImage(UIImage(named: "study_correct"), for: UIControl.State.normal)
            _updatePoseCorrectTip()
        default:
            button.backgroundColor = UIColor(hexString: "#FF7A46", alpha: 1.0)
            button.setImage(UIImage(named: "study_error"), for: UIControl.State.normal)
            _updatePoseWrongTip()
        }
    }
    
    // 点击手势
    @objc func _handleTap() {
        onTap()
    }
}
